import sys
from qtpy import QtCore
from qtpy import QtWidgets
from qtpy.QtCore import Qt, QRect


class EmittingStr(QtCore.QObject):
    textWritten = QtCore.Signal(str)
    def write(self, text):
    #   text = f"({os.getcwd()})=> {text}\n"
      self.textWritten.emit(text)
      loop = QtCore.QEventLoop()
      QtCore.QTimer.singleShot(10, loop.quit)
      loop.exec_()

    def flush(self):
        pass


class ProgressBar(QtWidgets.QDialog):
    def __init__(self,  parent=None):
        super(ProgressBar, self).__init__(parent)
 
        # Qdialog窗体的设置
        self.resize(500, 32) # QDialog窗的大小
 
        # 创建并设置 QProcessbar
        self.progressBar = QtWidgets.QProgressBar(self) # 创建
        # self.progressBar.setMinimum(0) #设置进度条最小值
        self.progressBar.setValue(0)  # 进度条初始值为0
        self.progressBar.setGeometry(QRect(1, 3, 499, 28)) # 设置进度条在 QDialog 中的位置 [左，上，右，下]
        self.show()
 
    def setValue(self, total_task_number, value): # 设置总任务进度和子任务进度
        self.progressBar.setMaximum(total_task_number)  # 设置进度条最大值
        label = "正在处理：" + "第" + str(value) + "/" + str(total_task_number)+'个任务'
        self.setWindowTitle(self.tr(label)) # 顶部的标题
        self.progressBar.setValue(value)
 
class SinglePoolBar():
    '''
    task_number和 total_task_number都为 0 时，不显示当前进行的任务情况
    task_number<total_task_number 都为整数，错误的设置将出现错误显示，暂未设置报错警告
    
    # 使用示例
    import time
    bar = pyqtbar() # 创建实例
    total_number=10
    # 任务1
    task_id=1
    for process in range(1, 100):
        time.sleep(0.05)
        bar.set_value(task_id,total_number,process) # 刷新进度条
    # 任务2
    task_id = 2
    for process in range(1, 100):
        time.sleep(0.05)
        bar.set_value(task_id, total_number,process)
    bar.close # 关闭 bar 和 app
    '''
    def __init__(self):
        # self.app = QtWidgets.QApplication(sys.argv) # 打开系统 app
        self.progressbar = ProgressBar() # 初始化 ProcessBar实例
 
    def set_value(self, total_task_number, i):
        self.progressbar.setValue(total_task_number, i+1)  # 更新进度条的值
        QtWidgets.QApplication.processEvents()  # 实时刷新显示
 
    def close(self):
        self.progressbar.close()  # 关闭进度条
        # self.app.exit() # 关闭系统 app


class DebugButton(QtWidgets.QWidget):
    def __init__(self,  parent=None):
        super(DebugButton, self).__init__(parent)
        # debug True
        self.debug_label = QtWidgets.QLabel("模拟还标：")
        self.set_debug_true = QtWidgets.QRadioButton("True")
        # self.set_debug_true.toggled.connect(self.set_debug)
        # debug False
        self.set_debug_false = QtWidgets.QRadioButton("False")
        self.set_debug_false.setChecked(True)
        # self.set_debug_false.toggled.connect(self.set_debug)
        debug_layout = QtWidgets.QHBoxLayout()
        debug_layout.addWidget(self.debug_label, 2)
        debug_layout.addWidget(self.set_debug_true, 2)
        debug_layout.addWidget(self.set_debug_false, 2)

        self.setLayout(debug_layout)


class PoolButton(QtWidgets.QWidget):
    def __init__(self,  parent=None):
        super(PoolButton, self).__init__(parent)
        # single pool
        self.single_pool_label = QtWidgets.QLabel("进程：")
        self.set_pool_single = QtWidgets.QRadioButton("Single Pool")
        self.set_pool_single.setChecked(True)
        # self.set_pool_single.toggled.connect(self.set_pool)
        # multi pool
        self.set_pool_multi = QtWidgets.QRadioButton("Multi Pool")
        # self.set_pool_multi.toggled.connect(self.set_pool)
        pool_layout = QtWidgets.QHBoxLayout()
        pool_layout.addWidget(self.single_pool_label, 2)
        pool_layout.addWidget(self.set_pool_single, 2)
        pool_layout.addWidget(self.set_pool_multi, 2)
        
        self.setLayout(pool_layout)

